package com.ruoyi.web.controller.race;

import java.util.List;
import java.util.Arrays;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.live.base.domain.LiveExam;
import com.ruoyi.live.base.service.ILiveExamService;
import com.ruoyi.live.configuration.SessionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.LiveExamTask;
import com.ruoyi.live.base.service.ILiveExamTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 比赛任务Controller
 *
 * @author ruoyi
 * @date 2023-02-21
 */
@Slf4j
@RestController
@RequestMapping("/race/examTask")
public class LiveExamTaskController extends BaseController
{
    @Autowired
    private ILiveExamTaskService liveExamTaskService;

    @Autowired
    private ILiveExamService liveExamService;

    /**
     * 查询比赛任务列表
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveExamTask liveExamTask)
    {
        startPage();
        QueryWrapper<LiveExamTask> wrapper = new QueryWrapper<>(liveExamTask);
        List<LiveExamTask> list = liveExamTaskService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出比赛任务列表
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:export')")
    @Log(title = "比赛任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LiveExamTask liveExamTask)
    {
        QueryWrapper<LiveExamTask> wrapper = new QueryWrapper<>(liveExamTask);
        List<LiveExamTask> list = liveExamTaskService.list(wrapper);
        ExcelUtil<LiveExamTask> util = new ExcelUtil<LiveExamTask>(LiveExamTask.class);
        util.exportExcel(response, list, "比赛任务数据");
    }

    /**
     * 获取比赛任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(liveExamTaskService.getById(id));
    }

    /**
     * 新增比赛任务
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:add')")
    @Log(title = "比赛任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveExamTask entity)
    {
        LiveExam exam = liveExamService.getById(entity.getExamId());
        if (exam.getState() != 0 && exam.getExamType() == 2) {
            log.error("比赛已经开始，无法添加任务");
            return error("比赛已经开始，无法添加任务");
        }
        else {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setCreateUser(SessionInfo.getUserId());
            entity.setState(1);
            return toAjax(liveExamTaskService.save(entity));
        }
    }

    /**
     * 修改比赛任务
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:edit')")
    @Log(title = "比赛任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveExamTask entity)
    {
        if (entity.getState() != 0) {
            log.error("比赛已经开始，无法修改任务");
            return error("比赛已经开始，无法修改任务");
        }
        else {
            return toAjax(liveExamTaskService.updateById(entity));
        }
    }

    /**
     * 删除比赛任务
     */
    @PreAuthorize("@ss.hasPermi('race:examTask:remove')")
    @Log(title = "比赛任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);

        LiveExamTask task = liveExamTaskService.getById(ids[0]);
        LiveExam exam = this.liveExamService.getById(task.getExamId());
        if (exam.getState() != 0) {
            log.error("比赛已经开始，无法删除任务");
            return error("比赛已经开始，无法删除任务");
        } else {
            return toAjax(liveExamTaskService.removeByIds(list));
        }
    }
}